package com.frinder.official.utils;

import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.util.zip.CRC32;

/**
 * Created by frinder on 2017/9/30.
 */
@Slf4j
public class Utils {

    /**
     * get CRC32 remainder
     *
     * @param key
     * @param divisor
     * @return
     */
    public static long remainder(String key, int divisor) {
        CRC32 crc32 = new CRC32();
        crc32.update(String.valueOf(key).toUpperCase().getBytes(Charset.forName("UTF-8")));
        long dividend = crc32.getValue();
        long remainder = dividend % divisor;
        log.debug("key: {}, divisor: {}, dividend: {}, remainder: {}",
                key,
                divisor,
                dividend,
                remainder
        );
        return Math.abs(remainder);
    }

    public static void main(String[] args) {
        System.out.println(remainder("555", 4));
        System.out.println(remainder("111", 4));
        System.out.println(remainder("123", 4));
        System.out.println(remainder("aaa", 4));
    }

}
